package alarm;

import java.util.ArrayList;
import java.util.List;

public final class AlarmRegistry {
	private static AlarmRegistry registry = new AlarmRegistry();
	
	private List<Alarm> registeredAlarms = new ArrayList<Alarm>();
	
	private List<Boolean> actives = new ArrayList<Boolean>();
	
	public static AlarmRegistry getInstance() {
		return registry;
	}
	
	public void registerAlarm(Alarm alarm) {
		registeredAlarms.add(alarm);
		actives.add(true);
	}
	
	public void unregisterAlarm(Alarm alarm) {
		int index = -1;
		if((index = registeredAlarms.indexOf(alarm)) >= 0) {
			Alarm removed = registeredAlarms.remove(index);
			removed.unregister();
			
			actives.remove(index);
		}
	}
	
	public boolean isActive(int alarmIndex) {
		return actives.get(alarmIndex);
	}
	
	public boolean isActive(Alarm alarm) {
		int index = -1;
		if((index = registeredAlarms.indexOf(alarm)) >= 0) {
			return isActive(index);
		} else
			return false;
	}
	
	public void setActive(Alarm alarm, boolean isActive) {
		int index = -1;
		if((index = registeredAlarms.indexOf(alarm)) >= 0) {
			actives.set(index, isActive);
		}
	}

	public List<Alarm> getActiveAlarms() {
		List<Alarm> activesAlarms = new ArrayList<Alarm>();
		
		for (int i = 0; i < registeredAlarms.size(); i++) {
			if(actives.get(i))
				activesAlarms.add(registeredAlarms.get(i));
		}
		return activesAlarms;
	}
}
